package controller.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import common.entities.Msg;
import common.entities.Page;
import entities.login.User;
import entities.permission.Role;
import service.user.UserManageService;
import util.base.CurrentUser;
import util.encryption.Encryption;
/**
 * 用户管理Controller
 * @author hxl
 *
 */
@Controller
@RequestMapping("/userManage")
public class UserManageController {
	@Resource
	private UserManageService userManageService;
	/**
	 * 获得所有用户
	 * @return
	 */
	@RequestMapping(value="/getUsers")
	@ResponseBody
	public Page<User> getUsers(
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "20") int rows){
		return userManageService.getUsers(name,page,rows);
	}
	
	/**
	 * 新增或修改用户
	 * @return
	 */
	@RequestMapping(value="/saveUser")
	@ResponseBody
	@Transactional(rollbackFor=Exception.class)
	public Msg saveUser(
			@RequestParam(value = "id") String id,
			@RequestParam(value = "deptId",required=true) String deptId,
			@RequestParam(value = "roleIds") String roleIds,
			@RequestParam(value = "name",required=true) String name,
			@RequestParam(value = "password",required=true) String password,
			@RequestParam(value = "type",required=true ) int type){
		    Msg msg=new Msg("0","新增成功！");
		    //重名检验
		    if(userManageService.getUser(id,name)){
		    	msg.setInfo("用户名重复！");
				msg.setSerialNumber("-1");
				return msg;
		    }
		    List<Role>roles=new ArrayList<Role>();
		    for(String roleId:roleIds.split(",")){
		    	Role role=new Role();
		    	role.setId(roleId);
		    	roles.add(role);
		    }
		    String currentUser=CurrentUser.getCurrentUser().getName();
		    User user=new User();
		    user.setRoles(roles);
			if(type==0){
					user.setId(UUID.randomUUID().toString());
					user.setDeptId(deptId);
					user.setName(name);
					user.setPassword(Encryption.MD5Encrypt(password));
					user.setAdd_person(currentUser);
					user.setAdd_time(new Date());
					userManageService.addUser(user);
			}
			if(type==1){
				user.setId(id);
				user.setDeptId(deptId);
				user.setName(name);
				user.setPassword(Encryption.MD5Encrypt(password));
				user.setUpdate_person(currentUser);
				user.setUpdate_time(new Date());
				userManageService.updateUser(user);
				msg.setInfo("修改成功！");
				msg.setSerialNumber("2");
			}
			return msg;
	}
	/**
	 * 批量删除用户
	 * @return
	 */
	@RequestMapping(value="/deleteUser")
	@ResponseBody
	@Transactional(rollbackFor=Exception.class)
	public boolean deleteUser(@RequestParam(value = "ids") String ids){
	     userManageService.deleteUser(ids);
	     return true;
	}
	
}
